package com.ruoyi.pims.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.pims.domain.SalesOrderDetails;
import com.ruoyi.pims.service.ISalesOrderDetailsService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 销售订单明细Controller
 * 
 * @author ruoyi
 * @date 2025-06-17
 */
@Controller
@RequestMapping("/pims/details")
public class SalesOrderDetailsController extends BaseController
{
    private String prefix = "pims/details";

    @Autowired
    private ISalesOrderDetailsService salesOrderDetailsService;

    @RequiresPermissions("pims:details:view")
    @GetMapping()
    public String details()
    {
        return prefix + "/details";
    }

    /**
     * 查询销售订单明细列表
     */
    @RequiresPermissions("pims:details:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SalesOrderDetails salesOrderDetails)
    {
        startPage();
        List<SalesOrderDetails> list = salesOrderDetailsService.selectSalesOrderDetailsList(salesOrderDetails);
        return getDataTable(list);
    }

    /**
     * 导出销售订单明细列表
     */
    @RequiresPermissions("pims:details:export")
    @Log(title = "销售订单明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SalesOrderDetails salesOrderDetails)
    {
        List<SalesOrderDetails> list = salesOrderDetailsService.selectSalesOrderDetailsList(salesOrderDetails);
        ExcelUtil<SalesOrderDetails> util = new ExcelUtil<SalesOrderDetails>(SalesOrderDetails.class);
        return util.exportExcel(list, "销售订单明细数据");
    }

    /**
     * 新增销售订单明细
     */
    @RequiresPermissions("pims:details:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存销售订单明细
     */
    @RequiresPermissions("pims:details:add")
    @Log(title = "销售订单明细", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SalesOrderDetails salesOrderDetails)
    {
        return toAjax(salesOrderDetailsService.insertSalesOrderDetails(salesOrderDetails));
    }

    /**
     * 修改销售订单明细
     */
    @RequiresPermissions("pims:details:edit")
    @GetMapping("/edit/{itemId}")
    public String edit(@PathVariable("itemId") Long itemId, ModelMap mmap)
    {
        SalesOrderDetails salesOrderDetails = salesOrderDetailsService.selectSalesOrderDetailsByItemId(itemId);
        mmap.put("salesOrderDetails", salesOrderDetails);
        return prefix + "/edit";
    }

    /**
     * 修改保存销售订单明细
     */
    @RequiresPermissions("pims:details:edit")
    @Log(title = "销售订单明细", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SalesOrderDetails salesOrderDetails)
    {
        return toAjax(salesOrderDetailsService.updateSalesOrderDetails(salesOrderDetails));
    }

    /**
     * 删除销售订单明细
     */
    @RequiresPermissions("pims:details:remove")
    @Log(title = "销售订单明细", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(salesOrderDetailsService.deleteSalesOrderDetailsByItemIds(ids));
    }
}
